<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\OrdersComment;
use Illuminate\Http\Request;

class OrdersCommentController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = OrdersComment::from('orders_comment as oc')
            ->orderBy('oc.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'oc.user_id')
            ->leftJoin('goods', 'goods.id', '=', 'oc.goods_id')
            ->select([
                'oc.*', 'users.nickname', 'goods.name as goods_name',
                'goods.cover as goods_cover'
            ]);
        if ($nickname = $searchData['nickname'] ?? '') {
            $model->where('nickname', 'like', "%{$nickname}%");
        }
        if ($goods_name = $searchData['goods_name'] ?? '') {
            $model->where('goods.name', 'like', "%{$goods_name}%");
        }
        if ($content = $searchData['content'] ?? '') {
            $model->where('content', 'like', "%{$content}%");
        }

        $result = $model->paginate(config('app.limit'));

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }

    // 删除
    public function del(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        OrdersComment::destroy($id);
        return $this->writeJson(0, null, '删除成功');
    }
}
